﻿using System;
using System.Data;
using System.Text.RegularExpressions;

namespace FreeWorship
{
	public class VerseResult
	{
		private static int INIT_VALUE = -1;
		private String version=null;
		private Book book=null;
		private int chapter = INIT_VALUE;
		private int verseStart = INIT_VALUE ;
		private int verseEnd = INIT_VALUE ;
		private String title=null;
		
		public DataTable verseTable = null;
		
		public VerseResult(String version)
		{
			this.version = version;
		}
		
		private void SetTitle()
		{
			if(version=="CUV")
				title = book.chineseLongName+' '+chapter+":"+verseStart;
			else if(version=="NIV")
				title = TurnFirstToUpper(book.englishLongName)+' '+chapter+":"+verseStart;
			int maxVerseID=Convert.ToInt16(verseTable.Rows[verseTable.Rows.Count-1][0]);
			verseEnd=maxVerseID;
			if(verseStart < verseEnd)
				title += "-"+verseEnd;
		}
		
		private string TurnFirstToUpper(string input)
		{
			string temp = input.Substring(0,1);
			return temp.ToUpper() + input.Remove(0,1);
		}
		
		public String GetTitle()
		{
			if(title==null)
				SetTitle();
			return title;
		}
		
		public static VerseResult GetVerseResult(String keyword,String version)
		{
			string delimStr = " -:：——";
			
			char [] delimiter = delimStr.ToCharArray();
			string [] split = null;
			split = keyword.Split(delimiter);
			
			String bookName=null;
			int chapter=INIT_VALUE,verseStart=INIT_VALUE,verseEnd=INIT_VALUE;
			int index=0;
			try{
				while(split[index].Trim()=="") index++;
				bookName = split[index];
				while(split[index].Trim()=="") index++;
				chapter = int.Parse(split[1]);
				while(split[index].Trim()=="") index++;
				verseStart = int.Parse(split[2]);
				while(split[index].Trim()=="") index++;
				verseEnd = int.Parse(split[3]);
			}catch(System.IndexOutOfRangeException)
			{}
			catch(FormatException)
			{
				throw new KeywordException();
			}

			if(chapter==-1) chapter=1;
			if(verseStart==-1) {
				verseStart=1;
				verseEnd=9999;
			}
			if(verseEnd==-1) verseEnd=verseStart;
			else
				if(verseEnd==0)
					verseEnd=9999;
			
			VerseResult result = new VerseResult(version);
			
			result.book = GetBookID(bookName);
			result.chapter = chapter;
			result.verseStart=verseStart;
			result.verseEnd=verseEnd;
			
			String sql = string.Format("select verseid,content from verse where version='{4}' and bookid={0} and chapterid={1} and verseid>={2} and verseid<={3}", result.book.id,chapter,verseStart,verseEnd,version);
			result.verseTable=DBUtil.GetDataTable(sql);
			return result;
		}
		
		static Book GetBookID(String bookName)
		{
			DataTable dt = DBUtil.GetDataTable(string.Format("select id,clong,cshort,elong from book where clong like '{0}%' or cshort like '{0}%' or elong like '{0}%' or eshort like '{0}%' or pylong like '{0}%' or pyshort like '{0}%'",bookName));
			if (dt.Rows.Count ==0)
				throw new NoBookException();
			else if(dt.Rows.Count>1)
				throw new MultiBookException();
			Book book = new Book();
			book.id=Convert.ToInt32(dt.Rows[0][0]);
			book.chineseLongName=Convert.ToString(dt.Rows[0][1]);
			book.chineseShortName=Convert.ToString(dt.Rows[0][2]);
			book.englishLongName=Convert.ToString(dt.Rows[0][3]);
			return book;
		}
		
		public static Object[] GetBookList(String bookName)
		{
			DataTable dt = DBUtil.GetDataTable(string.Format("select clong from book where clong like '{0}%' or cshort like '{0}%' or elong like '{0}%' or eshort like '{0}%' or pylong like '{0}%' or pyshort like '{0}%'",bookName));
			Object[] bookArray=new Object[dt.Rows.Count];
			for(int i=0;i<bookArray.Length;i++)
			{
				bookArray[i]=dt.Rows[i][0];
			}
			return bookArray;
			
		}
	}
}
